package com.example.lin.recyclerviewtest_327ljd.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.lin.recyclerviewtest_327ljd.adapter.MyAdapter2;
import com.example.lin.recyclerviewtest_327ljd.bean.ArticleBean;
import com.example.lin.recyclerviewtest_327ljd.adapter.MyAdapter;
import com.example.lin.recyclerviewtest_327ljd.R;
import com.example.lin.recyclerviewtest_327ljd.bean.TcaseBean;
import com.example.lin.recyclerviewtest_327ljd.iface.ArticleListener;
import com.example.lin.recyclerviewtest_327ljd.iface.TcaseListener;
import com.example.lin.recyclerviewtest_327ljd.model.ArticleModel;
import com.example.lin.recyclerviewtest_327ljd.model.TcaseModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;//显示布局效果
    //private MyAdapter myAdapter;//适配器
    //private List<ArticleBean> list=null;//数据源

    private MyAdapter2 myAdapter2;
    private List<TcaseBean> list2=null;

    /*private ArticleListener articleListener=new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            list=beanlist;//得到网络数据
            myAdapter.setList(list);//传给adapter
            myAdapter.notifyDataSetChanged();//通知更新
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };*/
    private TcaseListener tcaseListener=new TcaseListener() {
        @Override
        public void onResponse(List<TcaseBean> belist) {
            list2=belist;
            myAdapter2.setList(list2);
            myAdapter2.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();

        //实例化ArticleModel,调用方法获取网络数据
        /*ArticleModel articleModel=new ArticleModel();
        articleModel.getResultList("article",
                                    2,
                                    "rv1l2tidbomann2qn86nln6ur2",
                                    articleListener);*/

        //实例化TcaseModel,调用方法获取网络数据
        TcaseModel tcaseModel=new TcaseModel();
        tcaseModel.getResultList("tcase",
                                  2,
                                 "rv1l2tidbomann2qn86nln6ur2",
                                  tcaseListener);
    }

    private void initRecyclerView() {
        //获取RecyclerView,设置属性，获取数据源，绑定
        recyclerView=(RecyclerView)findViewById(R.id.recyclervieew);
        //创建默认线性布局
        layoutManager=new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //固定每个item高度，提高性能
        recyclerView.setHasFixedSize(true);
        //获取数据并创建Adapter
        //list=getData();

        /*myAdapter=new MyAdapter(this);
        myAdapter.setList(list);
        //绑定RecyclerView和adapter
        recyclerView.setAdapter(myAdapter);*/

        myAdapter2=new MyAdapter2(this);
        myAdapter2.setList(list2);
        recyclerView.setAdapter(myAdapter2);
    }


/*
    //自定义：获取数据源并形成list
    private List<ArticleBean> getData(){
        //定义list,赋值，返回
        List<ArticleBean> list=new ArrayList<ArticleBean>();
        ArticleBean articleBean;

        //第1组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);

        //第2组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(2);
        articleBean.setTitle("标题2");
        articleBean.setDescription("描述2");
        articleBean.setTime("2017-4-2");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);

        //第3组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(3);
        articleBean.setTitle("标题3");
        articleBean.setDescription("描述3");
        articleBean.setTime("2017-4-3");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);

        //第4组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(4);
        articleBean.setTitle("标题4");
        articleBean.setDescription("描述4");
        articleBean.setTime("2017-4-4");
        articleBean.setImgid(R.drawable.f4);
        list.add(articleBean);

        //第5组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(5);
        articleBean.setTitle("标题5");
        articleBean.setDescription("描述5");
        articleBean.setTime("2017-4-5");
        articleBean.setImgid(R.drawable.f5);
        list.add(articleBean);

        //第6组模拟数据
        articleBean=new ArticleBean();
        articleBean.setArticleId(6);
        articleBean.setTitle("标题6");
        articleBean.setDescription("描述6");
        articleBean.setTime("2017-4-6");
        articleBean.setImgid(R.drawable.f6);
        list.add(articleBean);

        return  list;
    }*/
}
